<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件(分为头部和身体) -->
      <el-card class="box-card">
        <el-tabs v-model="activeName" v-loading="loading" class="tab-pane" @tab-click="handleClick">
          <el-tab-pane label="组织结构" name="first">
            <!-- 表头 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                    <el-dropdown>
                      <!-- 下拉菜单文字 -->
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <!-- 下拉项 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="add">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 树形控件 -->
            <el-tree
              :data="treeData"
              node-key="id"
              default-expand-all
              :expand-on-click-node="false"
              class="departments-tree"
            >
              <template #default="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  align="middle"
                  style="height: 40px; width: 100%"
                >
                  <!-- 部门名字 -->
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <!-- 功能操作区域 -->
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <!-- 两个内容 -->
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <!-- 下拉菜单 element -->
                        <el-dropdown>
                          <span> 操作<i class="el-icon-arrow-down" /> </span>
                          <!-- 下拉菜单 -->
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="add(data)">
                              添加子部门
                            </el-dropdown-item>
                            <el-dropdown-item @click.native="edit(data)">
                              编辑部门
                            </el-dropdown-item>
                            <el-dropdown-item @click.native="del(data)">
                              删除部门
                            </el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增子频道 -->
    <depart-Dialog
      ref="departDialog"
      :show-depart.sync="showDepartDialog"
      :simple-list="userSimpleList"
      :origin-list="treeData"
      :depart-id="departID"
      :edit="isEdit"
      @confirmEv="confirm"
    />
  </div>
</template>

<script>
import {
  getDepartInfoAPI,
  getUserSimpleAPI,
  addDepartmentAPI,
  getDepartDetailAPI,
  changeDepartDetailAPI,
  delDepartDetailAPI
} from '@/api'
import { transTree } from '@/utils'
import departDialog from '@/views/departments/components/departDialog.vue'
export default {
  name: 'Departments',
  // 局部注册组件
  components: {
    'depart-Dialog': departDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 显示添加部门弹窗
      showDepartDialog: false,
      // 部门负责人列表
      userSimpleList: [],
      // 当前部门id
      departID: '',
      // 编辑状态
      isEdit: false,
      loading: true
    }
  },
  created() {
    // 获取部门列表
    this.getDepartInfo()
    // 获取-部门负责人列表
    this.getUserSimple()
  },
  methods: {
    // 获取部门列表
    async getDepartInfo() {
      const res = await getDepartInfoAPI()
      console.log(res)
      this.treeData = transTree(res.data.depts, '')
      this.loading = false
    },
    // 获取部门负责人列表
    async getUserSimple() {
      const res = await getUserSimpleAPI()
      console.log(res)
      this.userSimpleList = res.data
    },
    // 确认按钮
    async confirm(departForm) {
      // 增加部门
      const title = this.$refs.departDialog.title
      if (title === '增加部门') {
        departForm.pid = this.departID
        // await等待此次执行成功再执行后面代码
        await addDepartmentAPI(departForm)
      } else if (title === '编辑部门') {
        // await等待此次执行成功再执行后面代码
        await this.changeDepartDetail(this.departID, departForm)
      } else if (title === '增加子部门') {
        departForm.pid = ''
        // await等待此次执行成功再执行后面代码
        await addDepartmentAPI(departForm)
      }
      // 刷新页面
      this.getDepartInfo()
      this.isEdit = false
    },
    // 获取部门详情
    async getDepartDetail(id) {
      const res = await getDepartDetailAPI(id)
      console.log(res)
      this.$refs.departDialog.form = res.data
    },
    // 修改部门详情
    async changeDepartDetail(id, form) {
      const res = await changeDepartDetailAPI(id, form)
      console.log(res)
    },
    // 删除部门
    async delDepartDetail(id) {
      const res = await delDepartDetailAPI(id)
      console.log(res)
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 正文部分-右侧的添加子部门
    add(data) {
      this.isEdit = false
      if (data.id === undefined) {
        this.$refs.departDialog.title = '增加子部门'
      } else {
        this.departID = data.id
        this.$refs.departDialog.title = '增加部门'
      }
      // 重置表单
      // const form = this.$refs.departDialog.form
      // for (const key in form) {
      //   form[key] = ''
      // }
      this.showDepartDialog = true
    },
    // 编辑部门
    async edit(data) {
      this.isEdit = true
      this.departID = data.id
      await this.getDepartDetail(this.departID)
      this.$refs.departDialog.title = '编辑部门'
      this.showDepartDialog = true
    },
    // 删除部门
    async del(data) {
      this.departID = data.id
      // await等待此次执行成功再执行后面代码
      await this.delDepartDetail(this.departID)
      this.getDepartInfo()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }

  .departments-tree {
    margin: 12px 0;
  }
}
</style>
